/**
 * 
 */
package com.cqan.mail.send.service.impl;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.mail.internet.MimeUtility;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

import com.cqan.mail.entity.MailEntity;
import com.cqan.mail.entity.MailEntity.User;
import com.cqan.mail.entity.config.MailConfig;
import com.cqan.mail.send.service.SendEmailService;

/**
 * @Description 实现各种邮件发送接口
 * @author slaton
 * @date 2012-5-16上午10:06:13
 *
 */
public class SendEmailServiceImpl implements SendEmailService{

	private MailConfig mailConfig;
	
	public Boolean sendSimpleEmail(MailEntity mail) {
		
		try {
			SimpleEmail email = new SimpleEmail();
			addBasicConfig(email);
			addUser(mail, email);
			email.setSubject(mail.getSubject());
			email.setMsg(mail.getMessage());
			email.send();
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public Boolean sendMultiPartEmail(MailEntity mail) {
		try {
			MultiPartEmail email = new MultiPartEmail();
			addBasicConfig(email);
			addUser(mail, email);
			List<EmailAttachment> atts = createAttachment(mail);
			for (EmailAttachment ea : atts) {
				email.attach(ea);
			}
			email.setSubject(mail.getSubject());
			email.setMsg(mail.getMessage());
			email.send();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	public Boolean sendHtmlEmail(MailEntity mail) {
		try {
			HtmlEmail email = new HtmlEmail();
			addBasicConfig(email);
			addUser(mail, email);
			List<EmailAttachment> atts = createAttachment(mail);
			for (EmailAttachment ea : atts) {
				email.attach(ea);
			}
			email.setSubject(mail.getSubject());
			email.setMsg(mail.getMessage());
			email.send();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 添加附件
	 * @param email
	 * @param mail
	 * @throws UnsupportedEncodingException 
	 */
	private List<EmailAttachment> createAttachment(MailEntity mail) throws UnsupportedEncodingException {
		List<File> files = mail.getAttachments();
		List<EmailAttachment> attachments = new ArrayList<EmailAttachment>();
		if (files!=null&&files.size()>0) {
			for (File file : files) {
				EmailAttachment ea = new EmailAttachment();
				ea.setDescription("");
				ea.setDisposition(EmailAttachment.ATTACHMENT);
				ea.setPath(file.getAbsolutePath());
				ea.setName(MimeUtility.encodeText(file.getName()));
				attachments.add(ea);
			}
		}
		return attachments;
	}

	

	/**
	 * 添加用户
	 * @param mail
	 * @param email
	 * @throws EmailException
	 */
	private void addUser(MailEntity mail, Email email)
			throws EmailException {
		//添加收件人
		List<User> tos = mail.getTo();
		if (tos!=null&&tos.size()>0) {
			for (User user : tos) {
				email.addTo(user.getEmail(), user.getName());
			}
		}
		//添加抄送人
		List<User> ccs = mail.getCc();
		if (ccs!=null&&ccs.size()>0) {
			for (User user : ccs) {
				email.addCc(user.getEmail(), user.getName());
			}
		}
		//添加密送人
		List<User> bccs = mail.getBcc();
		if (bccs!=null&&bccs.size()>0) {
			for (User user : bccs) {
				email.addBcc(user.getEmail(), user.getName());
			}
		}
	}
	
	private void addBasicConfig(Email email) throws EmailException{
		assertConfig();
		if (mailConfig.getSsl()) {
			email.setSSLOnConnect(Boolean.TRUE);
			email.setSslSmtpPort(String.valueOf(mailConfig.getSmtpPort()));
		}else{
			email.setSmtpPort(mailConfig.getSmtpPort());
		}
		email.setHostName(mailConfig.getSmtpHost());
		email.setAuthentication(mailConfig.getUserName(), mailConfig.getPassword());
		email.setSentDate(Calendar.getInstance().getTime());
		email.setFrom(mailConfig.getUserName(),mailConfig.getName());
		email.setCharset(mailConfig.getCharset());
	}
	
	private void assertConfig(){
		if (mailConfig==null) {
			throw new NullPointerException(" mailConfig is null ");
		}
	}

	
	public void setMailConfig(MailConfig mailConfig) {
		this.mailConfig = mailConfig;
	}

	
}
